package com.cb.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 *
 * @author cb
 * @version 1.0
 * @date 2020/6/20 11:34
 */
@Data
public class R {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "状态码")
    private int code;

    @ApiModelProperty(value = "响应消息")
    private String message;

    @ApiModelProperty(value = "响应数据")
    private Map<String, Object> data = new HashMap<>();

    private R(){}

    public static R ok(){
        R result = new R();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage("success");
        return result;
    }
    
    public static R error(){
        R result = new R();
        result.setSuccess(false);
        result.setCode(ResultCode.ERROR);
        result.setMessage("error");
        return result;
    }

    public R success(boolean flag){
        this.setSuccess(flag);
        return this;
    }

    public R code(int code){
        this.setCode(code);
        return this;
    }

    public R message(String message){
        this.setMessage(message);
        return this;
    }

    public R data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

}
